package com.xyhuo.leetcode.january;

/**
 * 解码方法
 * https://leetcode.cn/problems/decode-ways/
 *
 * @time 2024/1/3
 */
public class NumDecodings {

//    public static void main(String[] args) {
//        NumDecodings nd = new NumDecodings();
//        nd.numDecodings("2101");
//    }

    public int numDecodings(String s) {
        char[] charArray = s.toCharArray();
        if (charArray[0] == '0') {
            return 0;
        }
        int[] dp = new int[charArray.length];
        dp[0] = 1;
        for (int i = 1; i < charArray.length; i++) {
            int val = 0;
            if (charArray[i] != '0') {
                val += dp[i - 1];
            }
            if (charArray[i - 1] != '0' && (10 * (charArray[i - 1] - 48) + (charArray[i] - 48)) < 27) {
                if (i - 2 < 0) {
                    val += 1;
                } else {
                    val += dp[i - 2];
                }
            }
            dp[i] = val;
        }
        return dp[charArray.length - 1];
    }
}
